<?php

namespace app\ruinuo\service\database;
//自定义页面
use app\common\model\RuinuoDatabase as DataMode;
use app\common\service\DataBase as DataBaseService;

//当前模型
use think\facade\Config;
use think\facade\Db;

class Custom
{
    /*保存前处理*/
    public function savebefore($params = [], $id = 0)
    {
        if (empty($id)) {
            $table = Config::get('database.connections.mysql.prefix') . $params['a']['name'];
            $sql = "SHOW TABLES LIKE '" . $table . "'";
            if (count(Db::query($sql)) > 0) {
                return ['code' => 203, 'msg' => '数据库重复'];
            }
        }
        $DataBaseService = new DataBaseService();
        $DataBaseService->AddEdit($params['a']);
        return $params;
    }

    /*保存后处理*/
    public function saveafter($id = 0)
    {
        $Model = new DataMode();
        $find = $Model->where("id='" . $id . "'")->field("field")->findOrEmpty()->toArray();
        $field = !empty($find['field']) ? json_decode($find['field'], true) : [];
        $field[] = ['field' => 'id', 'title' => '主键', 'type' => 'int', 'length' => 11, 'key' => 1];
        $field[] = ['field' => 'create_time', 'title' => '新增时间', 'type' => 'datetime', 'length' => 0];
        $field[] = ['field' => 'update_time', 'title' => '修改时间', 'type' => 'datetime', 'length' => 0];
        $field[] = ['field' => 'create_id', 'title' => '新增人', 'type' => 'int', 'length' => 11, 'default' => 0];
        $field[] = ['field' => 'update_id', 'title' => '修改人', 'type' => 'int', 'length' => 11, 'default' => 0];
        $params['field'] = json_encode($field, JSON_UNESCAPED_UNICODE);
        $Model->where("id='" . $id . "'")->update($params);
    }

    /*列表查询前处理*/
    public function listbefore($params = [])
    {
        return $params;
    }

    /*列表查询后处理*/
    public function listafter($params = [])
    {
        return $params;
    }

    /*单条查询后处理*/
    public function oneafter($params = [])
    {
        if (!empty($params['detail'])) {
            $DataBase=new \app\common\service\DataBase();
            $field=$DataBase->Sync($params['detail']['name'],1);
            //$field = !empty($params['detail']['field']) ? json_decode($params['detail']['field'], true) : [];
            foreach ($field as $k => $v) {
                $field[$k]['old_field'] = $v['field'];
                if ($v['field'] == 'id' || $v['field'] == 'create_time' || $v['field'] == 'create_id' || $v['field'] == 'update_time' || $v['field'] == 'update_id' || $v['field'] == 'is_delete' || $v['field'] == 'tenant_id') {
                    unset($field[$k]);
                }
            }
            $params['detail']['old_name'] = $params['detail']['name'];
            $params['detail']['field'] = json_encode(array_values($field), JSON_UNESCAPED_UNICODE);
        }
        return $params;
    }

    /*删除前处理*/
    public function delbefore($del_id)
    {
        return $del_id;
    }

    /*删除后处理*/
    public function delafter($del_id)
    {
        return $del_id;
    }

    /*字段编辑前处理*/
    public function tablebefore($params = [])
    {
        return $params;
    }

    /*字段编辑后处理*/
    public function tableafter($params = [])
    {
        return $params;
    }

}